<?php

namespace app\common\service;

use app\common\enums\AccountTypeEnum;
use app\common\model\banxi\organization\Organization;
use app\common\model\banxi\student\Student;
use app\common\model\banxi\trainer\Trainer;

class AccountService extends ApiService
{
    /**
     * 获取账户信息
     */
    public function wrapperAccountInfo($data)
    {
        if (empty($data)) {
            return $data;
        }

        // 按 accountType 分组数据
        $accountTypeMap = [];
        foreach ($data as $item) {
            $accountTypeMap[$item['accountType']][] = $item;
        }

        // 为每种 accountType 获取对应的账户信息
        foreach ($accountTypeMap as $accountType => $accountBaseList) {
            $accountIds = array_unique(array_column($accountBaseList, 'accountId'));

            if (empty($accountIds)) {
                continue;
            }

            $accountInfoMap = [];
            switch ($accountType) {
                case AccountTypeEnum::ORGAN: // 机构
                    $accountInfoList = Organization::where([['id', 'in', $accountIds]])
                        ->field(['id', 'parentId', 'name'])
                        ->select();
                    break;
                case AccountTypeEnum::STUDENT: // 学生
                    $accountInfoList = Student::where([['id', 'in', $accountIds]])
                        ->field(['id', 'account', 'name', 'sex', 'avatar', 'age'])
                        ->select();
                    break;
                case AccountTypeEnum::TRAINER: // 教练
                    $accountInfoList = Trainer::where([['id', 'in', $accountIds]])
                        ->field(['id', 'account', 'name', 'sex', 'avatar', 'age'])
                        ->select();
                    break;
                default:
                    $accountInfoList = [];
                    break;
            }

            // 将账户信息转换为以 id 为键的数组，提高查找效率
            foreach ($accountInfoList as $accountInfo) {
                $accountInfoMap[$accountInfo['id']] = $accountInfo;
            }

            // 将账户信息关联到原始数据中
            foreach ($data as &$datum) {
                // 如果已经处理过 accountInfo，跳过
                if (isset($datum['accountInfo'])) {
                    continue;
                }

                // 检查是否匹配当前 accountType 和 accountId
                if ($datum['accountType'] == $accountType && isset($accountInfoMap[$datum['accountId']])) {
                    $datum['accountInfo'] = $accountInfoMap[$datum['accountId']];
                }
            }
            unset($datum); // 销毁引用
        }

        return $data;
    }


    /**
     * 获取单个账户信息
     */
    public function getAccountInfo($accountId, $accountType)
    {
        switch ($accountType) {
            case AccountTypeEnum::ORGAN: // 机构
                $accountInfo = Organization::where([['id', '=', $accountId]])
                    ->field(['id', 'parentId', 'name'])
                    ->find();
                break;
            case AccountTypeEnum::STUDENT: // 学生
                $accountInfo = Student::where([['id', '=', $accountId]])
                    ->field(['id', 'account', 'name', 'sex', 'avatar', 'age', 'organizationId','gradeId'])
                    ->find();
                break;
            case AccountTypeEnum::TRAINER: // 教练
                $accountInfo = Trainer::where([['id', '=', $accountId]])
                    ->field(['id', 'account', 'name', 'sex', 'avatar', 'age', 'organizationId'])
                    ->find();
                break;
            default:
                $accountInfo = [];
                break;
        }
        return $accountInfo;
    }
}
